<template>
  <div class="pc-home" v-if="!isphone">
    <div class="cntWrap">
      <div class="cnt">
        <div class="cartleft">
          <div class="left-top">
            <div class="left-title">
              购物袋
              <span><i>0</i>项</span>
            </div>

            <!-- <div class="left-login"></div> -->
          </div>

          <div class="cart-list">
            <div class="allchose">
              <input
                type="checkbox"
                class="all"
                :checked="allchose"
                @change="all"
              />全选
            </div>
            <div class="cartitem" v-for="item in carts" :key="item.id">
              <input
                type="checkbox"
                @change="total"
                v-model="idlist"
                :value="item.id"
              />
              <img :src="item.img[0]" alt="" />
              <div class="itemimf">
                <h1>{{ item.brand + item.name }}</h1>
                <span>ID:{{ item.id }}</span>
                <div class="corsize">
                  <div>颜色:默认色</div>
                  <div class="cart-count">
                    件数: <span>{{ item.count }}</span>
                  </div>
                </div>
                <van-stepper
                  v-model="item.count"
                  @plus="item.count++"
                  @minus="item.count--"
                />
                <a href="" @click="remove(item.id)">删除</a>
              </div>
              <span class="itemprice">￥{{ item.price }}</span>
            </div>
          </div>
        </div>

        <div class="cartright">
          <div class="order">
            <div class="ordertitle">
              订单摘要
              <span class="ordercount">0件</span>
            </div>
            <div class="ordermoney">
              <span>小计</span>
              <span>￥{{ sum / 100 }}</span>
            </div>
            <div class="orderdis">
              <span>优惠金额</span>
              <span>-￥0.00</span>
            </div>

            <div class="ordertotal">
              <span>订单总金额</span>
              <span>￥{{ sum / 100 }}</span>
            </div>
            <button @click="gopay(idlist)">结算</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div v-else class="phone-home">
    <van-nav-bar
      title="购物车"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <van-swipe-cell v-for="item in carts" :key="item.id">
      <template>
        <input
          type="checkbox"
          v-model="idlist"
          :value="item.id"
          @change="total"
        />
      </template>

      <van-card
        :num="item.count"
        :price="item.price"
        :desc="item.desc.slice(0, 13) + '...'"
        :title="item.name"
        :thumb="item.img[0]"
      >
        <template #footer>
          <van-stepper
            v-model="item.count"
            @plus="item.count++"
            @minus="item.count--"
            theme="round"
            button-size="22"
            disable-input
          />
        </template>
      </van-card>
      <template #right>
        <van-button
          square
          text="删除"
          type="danger"
          class="delete-button"
          @click="remove(item.id)"
        />
      </template>
    </van-swipe-cell>
    <van-submit-bar :price="sum" button-text="提交订单" @submit="gopay(idlist)">
      <input type="checkbox" :checked="allchose" @change="all" />
      <template #tip> 价格昂贵,理性消费 </template>
    </van-submit-bar>
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
export default {
  data() {
    return {
      idlist: [],
      sum: 0,
      allchose: false,
      needpay: [],
    };
  },
  created() {
    // this.carts = JSON.parse(localStorage.getItem('carts')) || [];
  },
  methods: {
    total() {
      console.log(this.idlist);
      if (this.idlist.length == this.carts.length) {
        this.allchose = true;
      } else {
        this.allchose = false;
      }
      this.getsum();
    },
    all() {
      var arr = [];
      this.carts.forEach((item) => {
        arr.push(item.id);
      });
      this.idlist = this.idlist.length == this.carts.length ? [] : arr;
      this.getsum();
    },
    getsum() {
      this.sum = 0;
      for (let i = 0; i < this.idlist.length; i++) {
        this.carts.forEach((item) => {
          if (item.id == this.idlist[i]) {
            this.sum += item.count * item.price * 100;
          }
        });
      }
    },
    plus(id) {
      var index = this.carts.findIndex((item) => {
        item.id = id;
      });
      this.carts[index].count++;
    },
    minus(id) {
      var index = this.carts.findIndex((item) => {
        item.id = id;
      });
      this.carts[index].count--;
    },
    gopay(obj) {
      this.needpay = [];
      for (let i = 0; i < obj.length; i++) {
        this.carts.forEach((item) => {
          if (item.id == obj[i]) {
            this.needpay.push(item);
          }
        });
      }
      this.pay(this.needpay);
      this.$router.push('/order');
      // console.log(this.needpay);
    },
    ...mapMutations('carts', ['pay']),
    ...mapMutations('carts', ['remove']),
  },
  watch: {
    carts: {
      deep: true,
      handler(nv) {
        console.log(nv);
        this.sum = 0;
        this.getsum();
      },
    },
  },
  computed: {
    // 取出media中的isphone 其他页面按此方法取值
    // isphone为true时，手机端模板，否则网页端模板
    ...mapState('media', ['isphone']),
    ...mapState('carts', ['carts']),
  },
};
</script>
<style lang="scss" scoped>
.pc-home {
  .cntWrap {
    margin-top: 70px;
    width: 100%;
    min-height: 600px;
    padding-top: 50px;
    background-color: #f7f8fa;

    .cnt {
      width: 1200px;
      min-height: auto;
      margin: 0 auto;

      display: flex;
      justify-content: space-between;

      .cartleft {
        min-height: 600px;
        height: 100%;
        width: 63%;
        background-color: white;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

        .left-top {
          height: 50px;
          width: 100%;
          padding-left: 15px;

          font-size: 22px;
          font-weight: bold;
          line-height: 50px;

          span {
            font-size: 14px;
            color: #919090;
          }
        }

        .left-bg {
          margin: 0 auto;
          height: 150px;
          width: 95%;
          background: url('@/assets/测试素材/slider1.jpg');
          background-size: 100%;
          text-align: center;

          h1 {
            line-height: 150px;
            font-size: 22px;
            font-weight: 600;
            color: white;
          }
        }

        .cart-list {
          width: 95%;

          margin: 0 auto;

          .allchose {
            width: 100%;
            height: 40px;
            line-height: 40px;

            border-bottom: 1px solid #e2e2e2;
            font-size: 15px;
            margin-bottom: 10px;

            input {
              margin-right: 10px;
            }
          }

          .cartitem {
            height: 150px;
            border-bottom: 1px solid #e2e2e2;
            margin-bottom: 10px;

            display: flex;
            align-items: center;
            justify-content: space-around;

            img {
              width: 100px;
            }

            .itemimf {
              height: 100%;
              width: 60%;
              padding: 10px;

              display: flex;
              flex-direction: column;
              justify-content: space-between;

              h1 {
                line-height: 22px;
                font-size: 16px;
                color: #000000;
                font-weight: 600;
                margin-bottom: 5px;
              }

              span {
                color: #616265;
                font-size: 14px;
                font-weight: 600;
                line-height: 18px;
              }

              .corsize {
                margin-top: 5px;
                display: flex;
                justify-content: space-between;
                font-size: 14px;
                color: #616265;
              }

              a {
                color: rgb(231, 82, 82);
                font-size: 14px;
              }
            }

            .itemprice {
              font-size: 20px;
              color: #000000;
              font-weight: 600;
            }
          }
        }
      }

      .cartright {
        min-height: 600px;
        width: 35%;

        .discount {
          height: 130px;
          width: 100%;
          background-color: white;
          box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
          padding: 30px;
          margin-bottom: 30px;

          h1 {
            font-size: 18px;
            line-height: 1;
            color: #000000;
            height: 36px;
            border: none;
          }

          div {
            border-bottom: 1px solid #c6bfbf;
            display: flex;

            input {
              height: 30px;
              width: 70%;
              border: none;
            }

            button {
              height: 30px;
              width: 30%;
              border: none;
              background-color: #c6bfbf;
            }
          }
        }

        .order {
          width: 100%;
          height: 350px;
          padding: 30px;
          background-color: white;
          box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
          .ordertitle {
            font-size: 22px;
            font-weight: bold;
            line-height: 50px;
            border-bottom: 1px solid #c6bfbf;

            span {
              font-size: 14px;
              color: #919090;
            }
          }
          .ordermoney {
            display: flex;
            justify-content: space-between;
            border-bottom: 1px solid #c6bfbf;
            line-height: 44px;
            font-weight: 600;
            color: #000000;
            font-size: 18px;
          }
          .orderdis {
            display: flex;
            justify-content: space-between;
            border-bottom: 1px solid #c6bfbf;
            line-height: 44px;
            font-weight: 400;
            color: #616265;
            font-size: 16px;
          }
          .ordertotal {
            display: flex;
            justify-content: space-between;
            line-height: 44px;
            font-weight: 600;
            color: #000000;
            font-size: 18px;
          }
          button {
            height: 40px;
            width: 140px;
            background-color: #0063d1;
            color: white;
            margin: 40px 0 0 110px;
          }
        }
      }
    }

    .carttj {
      height: 400px;
      width: 1248px;
      padding-top: 50px;
      margin: 0 auto;

      h1 {
        line-height: 60px;
        color: #000000;
        font-size: 28px;
        font-weight: 600;
        text-align: center;
        margin-bottom: 10px;
      }

      .tab-tj {
        width: 80%;
        margin: 0 auto;
        display: flex;
        justify-content: center;

        .tab-tj-pdt {
          width: 180px;
          height: 230px;
          margin-right: 20px;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: space-around;
          font-size: 14px;

          img {
            width: 180px;
            margin-bottom: 5px;
          }
        }
      }
    }
  }
}

.phone-home {
  .delete-button {
    height: 100%;
  }

  .van-swipe-cell__wrapper {
    input {
      z-index: 10;
      position: absolute;
      left: 10px;
      top: 40%;
      border-radius: 50%;
    }

    .van-card__header {
      padding-left: 20px;
    }
  }

  .list {
    padding: 10px;

    .box {
      box-sizing: border-box;
      align-items: center;
      border: 1px solid #eee;
      margin-bottom: 10px;
      width: 100%;
      display: flex;
      justify-content: space-around;
      align-items: center;
      padding: 5px 20px;

      .left {
        flex: 20%;
      }

      .right {
        flex: 80%;

        .van-card {
          height: 120px;
        }
      }
    }
  }
}
</style>
